package com.zhaoxg.es.service.impl;

import com.zhaoxg.es.pojo.Product;
import com.zhaoxg.es.service.SearchService;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHitSupport;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhaoxg on 2023年02月26日 15:24
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired(required=true)
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    // 价格排序-从高到低
    public final static String HIGH = "H";
    // 价格培训-从低到高
    public final static String LOW = "L";

    @Override
    public Map search(Map<String, String> paramMap) {
        Map<String,Object> resultMap = new HashMap<>();
        int currentPage = Integer.parseInt(paramMap.get("currentPage"));
        int pageSize = Integer.parseInt(paramMap.get("pageSize"));
        //设置查询分页
        PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
        //关键字搜索
        String keywords = paramMap.get("keywords");
        if (!StringUtils.isEmpty(keywords)) {
            builder.withQuery(QueryBuilders.termQuery("title", keywords));
        }
        builder.withPageable(pageRequest);
        //价格排序
        String isAscending = paramMap.get("isAscending");
        if (!StringUtils.isEmpty(isAscending)) {
            if (HIGH.equals(isAscending)) {
                builder.withSort(SortBuilders.fieldSort("price").order(SortOrder.DESC));
            } else if (LOW.equals(isAscending)) {
                builder.withSort(SortBuilders.fieldSort("price").order(SortOrder.ASC));
            }
        }
        //高亮
        builder.withHighlightFields(new HighlightBuilder.Field("title").preTags("<span style='color:red'>").postTags("</span>"));
        NativeSearchQuery searchQuery = builder.build();
        SearchHits<Product> searchHits = elasticsearchRestTemplate.search(searchQuery, Product.class);
        SearchPage<Product> searchPage = SearchHitSupport.searchPageFor(searchHits, searchQuery.getPageable());
        //总记录数
        resultMap.put("total", searchPage.getTotalElements());
        //总页数
        resultMap.put("totalPages", searchPage.getTotalPages());
        //数据集合
        resultMap.put("rows", searchPage.getContent());
        //当前页
        resultMap.put("currentPage", currentPage);
        return resultMap;
    }
}
